/**
 * Copyright (C) 2013 DaiKit.com - daikit4gxt module (admin@daikit.com)
 *
 *         Project home : http://code.daikit.com/daikit4gxt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.daikit.daikit4gxt.client.action.base;

import com.daikit.commons.shared.bean.DkBaseUser;
import com.daikit.commons.shared.exception.authentication.DkUserLoggedChangedException;
import com.daikit.daikit4gxt.client.DkBaseMain;
import com.daikit.daikit4gxt.client.action.DkBaseAction;
import com.daikit.daikit4gxt.client.action.DkBaseAsyncCallback;
import com.daikit.daikit4gxt.client.action.DkChainActionCreator;


/**
 * Action called when client receive a {@link DkUserLoggedChangedException} from the server. It show a popup asking if
 * the user wants to reload the UI based on the new logged user according to the server. <br>
 * This can happen when this application is linked to others (via SSO for example) and the user disconnected and
 * reconnected in the other application.
 * 
 * @author tcaselli
 * @version $Revision$ Last modifier: $Author$ Last commit: $Date$
 */
public class DkBaseOnServerLoggedUserChangedAction
{

	/**
	 * A new instance of this action.
	 * 
	 * @param chainAction
	 *           the chain action to be executed once the application has synchronized its logged user with the server
	 * @return a the action to retrieve new logged user from the server and to reload data according to it (and the given
	 *         base action is added at the end of the chain)
	 */
	public static DkBaseAction<?> get(final DkBaseAction<?> chainAction)
	{
		final DkBaseUser previousLoggedUser = DkBaseMain.model().getUserLogged();

		final DkBaseAction<DkBaseUser> actionLoadSynchronizedLoggedUser = new DkBaseAction<DkBaseUser>("")
		{
			@Override
			protected void run()
			{
				DkBaseMain.controller().baseRpcUser().getSynchronizedCurrentUser(new DkBaseAsyncCallback<DkBaseUser>(this, true)
				{
					@Override
					public void processSuccess(final DkBaseUser result)
					{
						DkBaseMain.model().setUserLogged(result);
					}
				});
			}
		};
		// create different action according to the current logged user.
		actionLoadSynchronizedLoggedUser.setLastChainActionCreator(new DkChainActionCreator()
		{
			@Override
			public DkBaseAction<?> getCreatedActions()
			{
				final DkBaseUser loggedUser = DkBaseMain.model().getUserLogged();
				DkBaseAction<?> afterLoginChainAction = null;
				if (loggedUser != null)
				{
					afterLoginChainAction = chainAction != null ? chainAction : DkBaseAfterConnectAction.get(previousLoggedUser);
				}
				return afterLoginChainAction;
			}
		});

		return actionLoadSynchronizedLoggedUser;

	}
}
